package com.hfzy.ihk.web.ivr.biz;

import com.hfzy.ihk.facade.ivr.enums.CallInSource;
import com.hfzy.ihk.facade.ivr.vo.CallInto;
import com.hfzy.ihk.web.ivr.context.DisruptorInitBean;
import com.hfzy.ihk.web.ivr.support.callin.event.CallIntoEvent;
import com.hfzy.ihk.web.ivr.support.disruptor.DisruptorSupport;
import com.lmax.disruptor.RingBuffer;
import org.freeswitch.esl.client.transport.event.EslEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Created by Administrator on 2019/5/21.
 */
@Lazy
@Component
public class CallIntoBiz extends DisruptorSupport<CallInto, CallIntoEvent> {

    private static final Logger logger = LoggerFactory.getLogger(CallIntoBiz.class);

    @Override
    protected RingBuffer<CallIntoEvent> useRingBuffer(CallInto data) {
        RingBuffer ringBuffer = null;
        String useType = "Freeswitch呼入";
        ringBuffer = DisruptorInitBean.getRingBufferFreeswitchCallInto();
        checkRingBufferUse(ringBuffer, useType);
        return ringBuffer;
    }

    /**
     * 进入呼入队列
     * @param event
     */
    public void freeswitchCallIntoOpt(EslEvent event){
        Map<String, String> eventHeaders = event.getEventHeaders();
        CallInto callInto = new CallInto();
        callInto.setMainCallPhone(eventHeaders.get("Caller-Caller-ID-Number"));
        callInto.setSessionsId(eventHeaders.get("Channel-Call-UUID"));
        callInto.setShowNum(eventHeaders.get("variable_sip_to_user"));
        callInto.setCallInSource(CallInSource.FREESWITCH);
        putInRingBuffer(callInto);
    }
}
